using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace DataAccess
{
	/// <summary>
	/// Strongly-typed collection for the TblContract class.
	/// </summary>
    [Serializable]
	public partial class TblContractCollection : ActiveList<TblContract, TblContractCollection>
	{	   
		public TblContractCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>TblContractCollection</returns>
		public TblContractCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                TblContract o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the TblContract table.
	/// </summary>
	[Serializable]
	public partial class TblContract : ActiveRecord<TblContract>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public TblContract()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public TblContract(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public TblContract(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public TblContract(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("TblContract", TableType.Table, DataService.GetInstance("mainConnection"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "ID";
				colvarId.DataType = DbType.Guid;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = false;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarContractDate = new TableSchema.TableColumn(schema);
				colvarContractDate.ColumnName = "ContractDate";
				colvarContractDate.DataType = DbType.DateTime;
				colvarContractDate.MaxLength = 0;
				colvarContractDate.AutoIncrement = false;
				colvarContractDate.IsNullable = true;
				colvarContractDate.IsPrimaryKey = false;
				colvarContractDate.IsForeignKey = false;
				colvarContractDate.IsReadOnly = false;
				colvarContractDate.DefaultSetting = @"";
				colvarContractDate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarContractDate);
				
				TableSchema.TableColumn colvarContractNumber = new TableSchema.TableColumn(schema);
				colvarContractNumber.ColumnName = "ContractNumber";
				colvarContractNumber.DataType = DbType.String;
				colvarContractNumber.MaxLength = 50;
				colvarContractNumber.AutoIncrement = false;
				colvarContractNumber.IsNullable = true;
				colvarContractNumber.IsPrimaryKey = false;
				colvarContractNumber.IsForeignKey = false;
				colvarContractNumber.IsReadOnly = false;
				colvarContractNumber.DefaultSetting = @"";
				colvarContractNumber.ForeignKeyTableName = "";
				schema.Columns.Add(colvarContractNumber);
				
				TableSchema.TableColumn colvarVillageId = new TableSchema.TableColumn(schema);
				colvarVillageId.ColumnName = "VillageId";
				colvarVillageId.DataType = DbType.Guid;
				colvarVillageId.MaxLength = 0;
				colvarVillageId.AutoIncrement = false;
				colvarVillageId.IsNullable = false;
				colvarVillageId.IsPrimaryKey = false;
				colvarVillageId.IsForeignKey = true;
				colvarVillageId.IsReadOnly = false;
				colvarVillageId.DefaultSetting = @"";
				
					colvarVillageId.ForeignKeyTableName = "TblVillage";
				schema.Columns.Add(colvarVillageId);
				
				TableSchema.TableColumn colvarAdvisorName = new TableSchema.TableColumn(schema);
				colvarAdvisorName.ColumnName = "AdvisorName";
				colvarAdvisorName.DataType = DbType.String;
				colvarAdvisorName.MaxLength = -1;
				colvarAdvisorName.AutoIncrement = false;
				colvarAdvisorName.IsNullable = true;
				colvarAdvisorName.IsPrimaryKey = false;
				colvarAdvisorName.IsForeignKey = false;
				colvarAdvisorName.IsReadOnly = false;
				colvarAdvisorName.DefaultSetting = @"";
				colvarAdvisorName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAdvisorName);
				
				TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
				colvarUserId.ColumnName = "UserId";
				colvarUserId.DataType = DbType.Guid;
				colvarUserId.MaxLength = 0;
				colvarUserId.AutoIncrement = false;
				colvarUserId.IsNullable = false;
				colvarUserId.IsPrimaryKey = false;
				colvarUserId.IsForeignKey = true;
				colvarUserId.IsReadOnly = false;
				colvarUserId.DefaultSetting = @"";
				
					colvarUserId.ForeignKeyTableName = "aspnet_Users";
				schema.Columns.Add(colvarUserId);
				
				TableSchema.TableColumn colvarContractType = new TableSchema.TableColumn(schema);
				colvarContractType.ColumnName = "ContractType";
				colvarContractType.DataType = DbType.Boolean;
				colvarContractType.MaxLength = 0;
				colvarContractType.AutoIncrement = false;
				colvarContractType.IsNullable = true;
				colvarContractType.IsPrimaryKey = false;
				colvarContractType.IsForeignKey = false;
				colvarContractType.IsReadOnly = false;
				colvarContractType.DefaultSetting = @"";
				colvarContractType.ForeignKeyTableName = "";
				schema.Columns.Add(colvarContractType);
				
				TableSchema.TableColumn colvarYear = new TableSchema.TableColumn(schema);
				colvarYear.ColumnName = "Year";
				colvarYear.DataType = DbType.Int32;
				colvarYear.MaxLength = 0;
				colvarYear.AutoIncrement = false;
				colvarYear.IsNullable = true;
				colvarYear.IsPrimaryKey = false;
				colvarYear.IsForeignKey = false;
				colvarYear.IsReadOnly = false;
				colvarYear.DefaultSetting = @"";
				colvarYear.ForeignKeyTableName = "";
				schema.Columns.Add(colvarYear);
				
				TableSchema.TableColumn colvarFourthFactorUserId = new TableSchema.TableColumn(schema);
				colvarFourthFactorUserId.ColumnName = "FourthFactorUserId";
				colvarFourthFactorUserId.DataType = DbType.Guid;
				colvarFourthFactorUserId.MaxLength = 0;
				colvarFourthFactorUserId.AutoIncrement = false;
				colvarFourthFactorUserId.IsNullable = true;
				colvarFourthFactorUserId.IsPrimaryKey = false;
				colvarFourthFactorUserId.IsForeignKey = false;
				colvarFourthFactorUserId.IsReadOnly = false;
				colvarFourthFactorUserId.DefaultSetting = @"";
				colvarFourthFactorUserId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFourthFactorUserId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["mainConnection"].AddSchema("TblContract",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("Id")]
		[Bindable(true)]
		public Guid Id 
		{
			get { return GetColumnValue<Guid>(Columns.Id); }
			set { SetColumnValue(Columns.Id, value); }
		}
		  
		[XmlAttribute("ContractDate")]
		[Bindable(true)]
		public DateTime? ContractDate 
		{
			get { return GetColumnValue<DateTime?>(Columns.ContractDate); }
			set { SetColumnValue(Columns.ContractDate, value); }
		}
		  
		[XmlAttribute("ContractNumber")]
		[Bindable(true)]
		public string ContractNumber 
		{
			get { return GetColumnValue<string>(Columns.ContractNumber); }
			set { SetColumnValue(Columns.ContractNumber, value); }
		}
		  
		[XmlAttribute("VillageId")]
		[Bindable(true)]
		public Guid VillageId 
		{
			get { return GetColumnValue<Guid>(Columns.VillageId); }
			set { SetColumnValue(Columns.VillageId, value); }
		}
		  
		[XmlAttribute("AdvisorName")]
		[Bindable(true)]
		public string AdvisorName 
		{
			get { return GetColumnValue<string>(Columns.AdvisorName); }
			set { SetColumnValue(Columns.AdvisorName, value); }
		}
		  
		[XmlAttribute("UserId")]
		[Bindable(true)]
		public Guid UserId 
		{
			get { return GetColumnValue<Guid>(Columns.UserId); }
			set { SetColumnValue(Columns.UserId, value); }
		}
		  
		[XmlAttribute("ContractType")]
		[Bindable(true)]
		public bool? ContractType 
		{
			get { return GetColumnValue<bool?>(Columns.ContractType); }
			set { SetColumnValue(Columns.ContractType, value); }
		}
		  
		[XmlAttribute("Year")]
		[Bindable(true)]
		public int? Year 
		{
			get { return GetColumnValue<int?>(Columns.Year); }
			set { SetColumnValue(Columns.Year, value); }
		}
		  
		[XmlAttribute("FourthFactorUserId")]
		[Bindable(true)]
		public Guid? FourthFactorUserId 
		{
			get { return GetColumnValue<Guid?>(Columns.FourthFactorUserId); }
			set { SetColumnValue(Columns.FourthFactorUserId, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public DataAccess.TblTaskCollection TblTasks()
		{
			return new DataAccess.TblTaskCollection().Where(TblTask.Columns.ContractId, Id).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a AspnetUser ActiveRecord object related to this TblContract
		/// 
		/// </summary>
		public DataAccess.AspnetUser AspnetUser
		{
			get { return DataAccess.AspnetUser.FetchByID(this.UserId); }
			set { SetColumnValue("UserId", value.UserId); }
		}
		
		
		/// <summary>
		/// Returns a TblVillage ActiveRecord object related to this TblContract
		/// 
		/// </summary>
		public DataAccess.TblVillage TblVillage
		{
			get { return DataAccess.TblVillage.FetchByID(this.VillageId); }
			set { SetColumnValue("VillageId", value.Id); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varId,DateTime? varContractDate,string varContractNumber,Guid varVillageId,string varAdvisorName,Guid varUserId,bool? varContractType,int? varYear,Guid? varFourthFactorUserId)
		{
			TblContract item = new TblContract();
			
			item.Id = varId;
			
			item.ContractDate = varContractDate;
			
			item.ContractNumber = varContractNumber;
			
			item.VillageId = varVillageId;
			
			item.AdvisorName = varAdvisorName;
			
			item.UserId = varUserId;
			
			item.ContractType = varContractType;
			
			item.Year = varYear;
			
			item.FourthFactorUserId = varFourthFactorUserId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varId,DateTime? varContractDate,string varContractNumber,Guid varVillageId,string varAdvisorName,Guid varUserId,bool? varContractType,int? varYear,Guid? varFourthFactorUserId)
		{
			TblContract item = new TblContract();
			
				item.Id = varId;
			
				item.ContractDate = varContractDate;
			
				item.ContractNumber = varContractNumber;
			
				item.VillageId = varVillageId;
			
				item.AdvisorName = varAdvisorName;
			
				item.UserId = varUserId;
			
				item.ContractType = varContractType;
			
				item.Year = varYear;
			
				item.FourthFactorUserId = varFourthFactorUserId;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn IdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn ContractDateColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn ContractNumberColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn VillageIdColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn AdvisorNameColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn UserIdColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn ContractTypeColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn YearColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn FourthFactorUserIdColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"ID";
			 public static string ContractDate = @"ContractDate";
			 public static string ContractNumber = @"ContractNumber";
			 public static string VillageId = @"VillageId";
			 public static string AdvisorName = @"AdvisorName";
			 public static string UserId = @"UserId";
			 public static string ContractType = @"ContractType";
			 public static string Year = @"Year";
			 public static string FourthFactorUserId = @"FourthFactorUserId";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
